package com.boho.base.dao;

/**
 * @File name:  DaoHelper.java
 * @Create on:  2010-5-10
 * @Author   :  陈伟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */


import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DAO;
import cn.boho.framework.po.DynaBeanMap;

import com.boho.base.cache.TSystemStatusCache;
import com.boho.base.cache.TdepartmentCache;
import com.boho.base.cache.TgfileCache;
import com.boho.base.cache.TpersonnalCache;
import com.boho.base.cache.TsuppliersCache;




/**
 * Dao辅助操作类
 * 
 */
public class DaoHelper extends DAO {
	/**
	 * 设置DynaBeanMap对象
	 * @param con
	 * @param dbm
	 * @param prefix
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setPicFile(Connection con,DynaBeanMap dbm, String prefix, String ext, String... key)throws Exception{
		if(key==null)
			return;
		if(dbm==null)
			return;
		DynaBeanMap dbmPicfile=null;
		StringBuffer picfile=null;
		for(int i=0;i<key.length;i++){
			String pic=(String)dbm.get(key[i]);
			if (pic==null || "".equals(pic)) {
				continue;
			}
			dbmPicfile=TgfileCache.getTgfileByPk(con,(String)dbm.get(key[i]));
			if(dbmPicfile!=null){
				String domainName=(String)dbmPicfile.get("DOMAIN_NAME");
				String fileCatalog=(String)dbmPicfile.get("FILE_CATALOG");
				String fileName=(String)dbmPicfile.get("FILE_NAME");
				if(prefix != null){
					fileName = prefix + fileName;
				}
				if(!(domainName==null||"".equals(domainName)
						||fileCatalog==null||"".equals(fileCatalog)
						||fileName==null||"".equals(fileName))){//路径完整
					picfile=new StringBuffer();
					picfile.append(domainName);
					picfile.append(fileCatalog);
					picfile.append("/");
					picfile.append(fileName);
					dbm.put(key[i] + ext, picfile.toString());
					continue;
				}
			}
			dbm.put(key[i], "");
		}
	}
	/**
	 * 设置DynaBeanMapList对象
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setPicFile(Connection con,List lst,String prefix, String ext, String... key)throws Exception{
		if(key==null)
			return;
		if(lst==null)
			return;
		int size = lst.size();
		for(int i=0;i<size;i++){
			DynaBeanMap dbm = (DynaBeanMap)lst.get(i);
			setPicFile(con,dbm,prefix,ext,key);
		}
	}
	
	/**
	 * 设置ComboPager对象
	 * @param con
	 * @param cp ComboPager对象
	 * @param prefix 图片前缀名称，如果没有传NULL
	 * @param ext 自定义扩展名， 防止覆盖原有字段属性
	 * @param key 查询关键字
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setPicFile(Connection con,ComboPager cp,String prefix, String ext, String... key)throws Exception{
		if(key==null)
			return;
		if(cp==null)
			return;
		List lst=cp.getRs();
		setPicFile(con,lst,prefix,ext,key);
	}
	/**
	 * 设置DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setDictField(Connection con, DynaBeanMap dbm, String statusType, String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		DynaBeanMap dbmDictField = null;
		/*
		String dictField = (String)dbm.get(key);
			if(dictField != null){
				dbmDictField = TSystemStatusCache.getTSystemStatusByPk(con, dictField, statusType);
				if(dbmDictField != null){
					dbm.put(key + ext, dbmDictField.get("TYPE_NAME"));
				}
			}
			*/
		for (int i = 0; i < key.length; i++) {
			if(dbm.get(key[i])==null||"".equals(dbm.get(key[i]))){
				dbm.put(key[i] + ext, "");
			}else{
				dbmDictField = TSystemStatusCache.getTSystemStatusByPk(con,dbm.get(key[i]).toString(),statusType);
				if(dbmDictField != null){
					dbm.put(key[i] + ext, dbmDictField.get("TYPE_NAME"));
				}else{dbm.put(key[i] + ext, "");}
			}
		}
	}

	/**
	 * 设置DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setDictField(Connection con, List lst,  String statusType, String ext, String key) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setDictField(con, dbm, statusType, ext, key);
		}
	}

	/**
	 * 设置ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param statusType 数据字典 状态类型
	 * @param key 查询关键字
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setDictField(Connection con, ComboPager cp,String statusType, String ext, String key) throws Exception {
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setDictField(con, lst,statusType, ext, key);
	}
	
	/**
	 * 设置DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiDictField(Connection con, DynaBeanMap dbm, String statusType, String ext, String key, String separator) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		DynaBeanMap dbmDictField = null;
		StringBuffer dbmBuffer = new StringBuffer();
		Map map = new HashMap<Object, Object>();//--update on 2009-11-16 去重
		String dictField = dbm.get(key).toString();
		if(dictField != null){
			String[] dictFieldArray = dictField.split(separator);
			for(int i = 0; i < dictFieldArray.length; i++){
				if(map.get(dictFieldArray[i])==null){//添加 map.get(dictFieldArray[i])==null
					dbmDictField = TSystemStatusCache.getTSystemStatusByPk(con, statusType, dictFieldArray[i]);
					if(dbmDictField != null){
						dbmBuffer.append(dbmDictField.get("TYPE_NAME"));
						map.put(dictFieldArray[i], dbmDictField.get("TYPE_NAME"));
					}
					if(i != dictFieldArray.length - 1){
						dbmBuffer.append(",");
					}
				}
			}
			dbm.put(key + ext, dbmBuffer.toString());
		}
	}
	
	/**
	 * 设置DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiDictField(Connection con, List lst,  String statusType, String ext, String key, String separator) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setMultiDictField(con, dbm, statusType, ext, key, separator);
		}
	}
	/**
	 * 设置ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param statusType 数据字典 状态类型
	 * @param key 查询关键字
	 * @param separator 元素分隔符
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiDictField(Connection con, ComboPager cp, String statusType, String ext, String key, String separator) throws Exception{
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setMultiDictField(con, lst,statusType, ext, key, separator);
	}
	
	/**
	 * 设置DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void putNewField(Connection con, DynaBeanMap dbm, DynaBeanMap dbmMap) throws Exception {
		if (dbmMap == null)
			return;
		if (dbm == null)
			return;
		dbm.putAll(dbmMap);
	}

	/**
	 * 设置DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void putNewField(Connection con, List lst, DynaBeanMap dbmMap) throws Exception {
		if (dbmMap == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			putNewField(con, dbm, dbmMap);
		}
	}

	/**
	 * 设置ComboPager对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void putNewField(Connection con, ComboPager cp, DynaBeanMap dbmMap) throws Exception {
		if (dbmMap == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		putNewField(con, lst, dbmMap);
	}
	

	
	/**
	 * 设置DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiField(Connection con, DynaBeanMap dbm, String statusType, String ext, String[] key) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		String[] str = new String[key.length];
		DynaBeanMap dbmInfo = null;
		for (int i = 0; i < key.length; i++) {
			str[i] = dbm.get(key[i]).toString();
		}
		for(int i = 0; i < str.length; i++){
			dbmInfo = TSystemStatusCache.getTSystemStatusByPk(con, str[i], statusType);
			if(dbmInfo != null){
				dbm.put(key[i] + ext, dbmInfo.get("TYPE_NAME"));
			}
		}
		
	}
	
	/**
	 * 设置DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiField(Connection con, List lst, String statusType,  String ext, String[] key) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setMultiField(con, dbm, statusType, ext, key);
		}
	}
	/**
	 * 设置ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param statusType 数据字典 状态类型
	 * @param key 查询关键字
	 * @param separator 元素分隔符
	 * @author 陈伟
	 */
	@SuppressWarnings("unchecked")
	public static void setMultiField(Connection con, ComboPager cp, String statusType, String ext, String[] key) throws Exception{
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setMultiField(con, lst, statusType, ext, key);
	}
	
	
	/**
	 * 设置人员信息DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTpersonField(Connection con, DynaBeanMap dbm, String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		DynaBeanMap dbmInfo = null;
		for (int i = 0; i < key.length; i++) {
			if(dbm.get(key[i])==null||"".equals(dbm.get(key[i]))){
				dbm.put(key[i] + ext, "");
			}else{
				dbmInfo = TpersonnalCache.getTpersonnalByPk(con, dbm.get(key[i]).toString());
				if(dbmInfo != null){
					dbm.put(key[i] + ext, dbmInfo.get("NAME"));
				}else{dbm.put(key[i] + ext, "");}
			}
		}
		
	}
	
	/**
	 * 设置人员信息DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTpersonField(Connection con, List lst,  String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setTpersonField(con, dbm, ext, key);
		}
	}
	/**
	 * 设置人员信息ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param key 查询关键字
	 * @param ext 替换后的后缀
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTpersonField(Connection con, ComboPager cp, String ext, String... key) throws Exception{
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setTpersonField(con, lst, ext, key);
	}
	
	/**
	 * 设置部门信息DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTDepartmentField(Connection con, DynaBeanMap dbm, String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		String[] str = new String[key.length];
		DynaBeanMap dbmInfo = null;
		for (int i = 0; i < key.length; i++) {
			if(dbm.get(key[i])==null||"".equals(dbm.get(key[i]))){
				dbm.put(key[i] + ext, "");
			}else{
				dbmInfo = TdepartmentCache.getTdepartment(con, dbm.get(key[i]).toString());
				if(dbmInfo != null){
					dbm.put(key[i] + ext, dbmInfo.get("DEPARTMENT_NAME"));
				}else{dbm.put(key[i] + ext, "");}
			}
		}
		
	}
	
	/**
	 * 设置部门信息DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTDepartmentField(Connection con, List lst,  String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setTDepartmentField(con, dbm, ext, key);
		}
	}
	/**
	 * 设置部门信息ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param key 查询关键字
	 * @param ext 替换后的后缀
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTDepartmentField(Connection con, ComboPager cp, String ext, String... key) throws Exception{
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setTDepartmentField(con, lst, ext, key);
	}
	
	/**
	 * 设置供应商信息DynaBeanMap对象
	 * 
	 * @param con
	 * @param dbm
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTsuppliersField(Connection con, DynaBeanMap dbm, String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (dbm == null)
			return;
		String[] str = new String[key.length];
		DynaBeanMap dbmInfo = null;
		for (int i = 0; i < key.length; i++) {
			if(dbm.get(key[i])==null||"".equals(dbm.get(key[i]))){
				dbm.put(key[i] + ext, "");
				
				dbm.put(key[i] + "_ADDRESS", "");
				dbm.put(key[i] + "_ASS_PHONE", "");
				dbm.put(key[i] + "_FAX", "");
				dbm.put(key[i] + "_CONTACT", "");
			}else{
				dbmInfo = TsuppliersCache.getTsuppliers(con, dbm.get(key[i]).toString());
				if(dbmInfo != null){
					dbm.put(key[i] + ext, dbmInfo.get("SUPPLIERS_NAME"));
									
					dbm.put(key[i] + "_ADDRESS", dbmInfo.get("ADDRESS"));
					dbm.put(key[i] + "_ASS_PHONE", dbmInfo.get("ASS_PHONE"));
					dbm.put(key[i] + "_FAX", dbmInfo.get("FAX"));
					dbm.put(key[i] + "_CONTACT", dbmInfo.get("CONTACT"));
					
				}else{dbm.put(key[i] + ext, "");}
			}
		}
		
	}
	
	/**
	 * 设置供应商信息DynaBeanMapList对象
	 * 
	 * @param con
	 * @param lst
	 * @param key
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTsuppliersField(Connection con, List lst,  String ext, String... key) throws Exception {
		if (key == null)
			return;
		if (lst == null)
			return;
		int size = lst.size();
		for (int i = 0; i < size; i++) {
			DynaBeanMap dbm = (DynaBeanMap) lst.get(i);
			setTsuppliersField(con, dbm, ext, key);
		}
	}
	/**
	 * 设置供应商信息ComboPager对象
	 * 
	 * @param con
	 * @param cp ComboPager对象
	 * @param key 查询关键字
	 * @param ext 替换后的后缀
	 * @author 许福辉
	 */
	@SuppressWarnings("unchecked")
	public static void setTsuppliersField(Connection con, ComboPager cp, String ext, String... key) throws Exception{
		if (key == null)
			return;
		if (cp == null)
			return;
		List lst = cp.getRs();
		setTsuppliersField(con, lst, ext, key);
	}

}
